package com.hcrazy.manchester.app.controllers.member;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcrazy.manchester.app.pojo.ErrorMsg;
import com.hcrazy.manchester.app.pojo.User;
import com.hcrazy.manchester.app.services.FeedbackService;
import com.hcrazy.manchester.app.utils.Constant;
import com.hcrazy.manchester.app.utils.Log;

/**
 * 消息部分
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/api")
public class FeedbackAPIController {
	
	@Autowired
	private FeedbackService feedbackService;
	
	/**
	 * 发送消息
	 * @param
	 * @return
	 */
	@RequestMapping(value="/feedback",method={RequestMethod.POST,RequestMethod.GET},produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object sendMessage(
			HttpServletRequest req,
			@RequestParam(required=false) String phone,
			@RequestParam(required=false) String email,
			@RequestParam String content,
			@RequestParam(required=false) String name
			){
		try {
			long userId = 0;
			//如果用户未登录，返回错误码
			Object obj = req.getSession().getAttribute("user");
			if(null != obj){
				User user = (User)obj;
				userId = user.getUserId();
			}
			
			feedbackService.addFeedback(phone, email, name, content, userId);
			return setResult("true");
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	
	
	/**
	 * 系统参数异常
	 * @param e
	 * @return
	 */
	@ExceptionHandler(Exception.class)
	public @ResponseBody ErrorMsg getException(Exception e){
		Log.error(e.getMessage(), e);
		return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
	}
	
	private Map<String,String> setResult(String value){
		Map<String,String> result = new HashMap<String,String>();
		result.put(Constant.SINGLE_RETURN,value);
		return result;
	}
}
